package com.beeasy.hzbpm.ctrl;

import cn.hutool.core.util.IdUtil;
import com.beeasy.hzbpm.util.Result;
import com.github.llyb120.nami.ctrl.Ctrl;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.ReplaceOptions;
import org.bson.Document;
import org.bson.types.ObjectId;

import static com.beeasy.hzbpm.bean.MongoService.db;
import static com.github.llyb120.json.Json.*;

public class office extends Ctrl {

    /**
     * 办公用品库
     * @return
     */
    public Object libList(){
return         db.getCollection("officeLib")
                .find()
            .into(a());
    }

    public Object editLib(){
        $request.ss("id", IdUtil.objectId());
        $request.remove("_id");
        db.getCollection("officeLib")
                .replaceOne(bo("id", $request.s("id")), bo($request), new ReplaceOptions().upsert(true));
        return true;
    }

    public Object rmLib(){
        db.getCollection("officeLib")
                .deleteOne(bo("id", $request.ss("id")));
        return true;
    }

    public Object one(String id){
        MongoCollection<Document> collection = db.getCollection("officeLib");
        Document doc = collection
                .aggregate(ba(
                        o("$match", o("_id", new ObjectId(id))),
                        o("$project", o(
                                "_id", o(
                                        "$toString", "$_id"
                                ),
                                "position",1,
                                "name",1,
                                "ps", 1
                        ))
                ))
                .first();
        return Result.ok(doc);
    }


}
